package com.paishen.peiniwan.module.date;

import android.app.Dialog;
import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import com.paishen.common.util.BaseUtil;
import com.paishen.peiniwan.R;

/**
 * Created by peng on 2015/12/4.
 */
public class DateGiftChooseDialog extends Dialog {

    public interface OnGiftSelectedListener {
        void onGiftSelected(long giftId, float giftPrice);
    }

    public DateGiftChooseDialog(Context context, int themeResId) {
        super(context, themeResId);
    }

    public static class Builder {
        private final Context context;
        private final boolean shadow = true;
        private final boolean canCancel = true;

        private DateGiftChooseLayout layDateGiftChoose;
        private OnGiftSelectedListener callback;

        public Builder setOnGiftSelectedListener(OnGiftSelectedListener callback) {
            this.callback = callback;
            return this;
        }

        public Builder(Context context) {
            this.context = context;
        }

        public DateGiftChooseDialog create() {
            final DateGiftChooseDialog dialog = new DateGiftChooseDialog(context, shadow ? R.style.Theme_Light_NoTitle_Dialog : R.style.Theme_Light_NoTitle_NoShadow_Dialog);
            Window window = dialog.getWindow();
            window.setWindowAnimations(com.paishen.common.R.style.Animation_Bottom_Rising);

            window.getDecorView().setPadding(0, 0, 0, 0);
            WindowManager.LayoutParams lp = window.getAttributes();
            lp.width = WindowManager.LayoutParams.MATCH_PARENT;
            lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
            window.setAttributes(lp);
            window.setGravity(Gravity.BOTTOM);

            View view = LayoutInflater.from(context).inflate(R.layout.dialog_date_gift_choose, null);
            layDateGiftChoose = (DateGiftChooseLayout) view.findViewById(R.id.lay_date_gift_choose);

            view.findViewById(R.id.tx_cancel).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dialog.dismiss();
                }
            });

            view.findViewById(R.id.tx_finish).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    long selectedGiftId = layDateGiftChoose.getGiftType();
                    float selectedGiftPrice = layDateGiftChoose.getGiftPrice();
                    if (selectedGiftId == DateGiftChooseLayout.TYPE_GIFT_UNSELECTED) {
                        BaseUtil.showToast(R.string.date_choose_gift);
                        return;
                    }

                    if (callback != null) callback.onGiftSelected(selectedGiftId, selectedGiftPrice);

                    dialog.dismiss();
                }
            });


            dialog.setContentView(view);
            dialog.setCanceledOnTouchOutside(canCancel);
            dialog.setCancelable(canCancel);
            return dialog;
        }
    }
}
